/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.gui.rendering;

import java.net.URL;

import net.groupme.model.Resource;
import net.groupme.model.ResourceVisualization;

/**
 * created on Jun 28, 2007
 * 
 * This is the default implementation of a {@link Renderer}. It simply lists all
 * attributes of a Resource as HTML.
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: mischa $
 * 
 * @version $Revision: 1.14 $ $Date: 2008-02-28 12:40:13 $
 */
public class DefaultRenderer extends AbstractRenderer implements Renderer {

	/* (non-Javadoc)
	 * @see net.groupme.gui.rendering.Renderer#renderAsHTML(net.groupme.model.ResourceVisualization)
	 */
	public String renderAsHTML(ResourceVisualization resourceVisualization, Integer groupWidth, Integer groupHeight, Integer zAxis) {
		String html = "";
		Resource resource = resourceVisualization.getResource();
		String width = "100px;";
		String widthMove = "100px;";
		if(resourceVisualization.getWidth() > 0){
			width = " width:" + (((resourceVisualization.getWidth()*groupWidth)/1000)-4) + "px;";
			widthMove = " width:" + (((resourceVisualization.getWidth()*groupWidth)/1000)) + "px;";
		}
		//div initialisierung
		html += this.getDivAttributes(resourceVisualization, groupWidth, groupHeight, zAxis);
		//menu:
		html += this.getMenu(resourceVisualization, resourceVisualization.getURI().toString(), groupWidth);
		//div open _content
		html += "<div id=\"" + resourceVisualization.getURI().toString() + "_content\" style=\"" + width + " min-width:130px; background:white; font-size:1.0em;\"> <strong>" + resource.getName() + "</strong>" + "<br/>";
		
		if(resource.getDescription() != null){
			html +=  "Description: " + resource.getDescription() ;
		}
		
		if(resource.getURL() != null){
			html += "<br/>URL: <a href=\"" + resource.getURL() +"\" target=\"_blank\""+ "\" title=\"visit resource @ " + resource.getURL() +"\">"
				 + formatURL(resource.getURL())
				 + "</a>";
		}
		// close div _content
		html += "</div>";
		// div open _tags
		html += "<div id=\"" + resourceVisualization.getURI().toString() + "_tags\" class=\"tag\" style=\"display:none;\">";
		// close div _tags
		html += "</div>";
		//close div URI
		html += "</div>";
		html += this.getMoveBar(resourceVisualization, widthMove);
		//close div _border
		html += "</div>";
		return html;
	}
	
	/**
	 * Transforms a URL to a string with a maximal length. Example:
	 * <pre>
	 * http://abc.de/bliblablup/bliblablup/feuerwehrmann.html
	 * </pre>
	 * ...will be transformed into:
	 * <pre>
	 * ..lup/feuerwehrmann.html
	 * </pre>
	 * @param url the url to be formatted
	 * @return a shortened version of the URL as a String
	 */
	private static final String formatURL(URL url){
		String formatted = url.toString(); 
		if(formatted.length() > 40){
			formatted = "..." + formatted.substring(formatted.length() - 39);
		}
		return formatted;
			
	}

}
